<template>
  <div class="user-profile">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>用户资料</span>
        </div>
      </template>
      
      <el-form :model="userForm" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="userForm.username" disabled />
        </el-form-item>
        
        <el-form-item label="手机号">
          <el-input v-model="userForm.phone" disabled />
        </el-form-item>
        
        <el-form-item label="头像">
          <AvatarUpload 
            :username="userForm.username" 
            :current-avatar="userForm.avatar"
            @avatar-updated="handleAvatarUpdated"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSave">保存</el-button>
          <el-button @click="$router.push('/')">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import AvatarUpload from '@/components/AvatarUpload.vue'
import request from '@/utils/request'

const userStore = useUserStore()

const userForm = reactive({
  username: '',
  phone: '',
  avatar: ''
})

// 获取用户信息
const getUserInfo = async () => {
  try {
    const username = userStore.username
    if (!username) {
      ElMessage.error('用户信息不存在')
      return
    }
    
    userForm.username = username
    
    // 获取用户详细信息（这里简化处理，实际应该调用API）
    // 暂时使用默认值
    userForm.phone = '13800138000' // 这里应该从API获取
    
    // 获取头像
    const response = await request.get('/user/getAvatar', {
      params: { username }
    })
    
    if (response.data.code === 200) {
      userForm.avatar = response.data.data
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
    ElMessage.error('获取用户信息失败')
  }
}

// 头像更新回调
const handleAvatarUpdated = (avatarUrl) => {
  userForm.avatar = avatarUrl
}

// 保存用户信息
const handleSave = () => {
  ElMessage.success('用户信息已更新')
}

onMounted(() => {
  getUserInfo()
})
</script>

<style scoped>
.user-profile {
  padding: 20px;
}

.profile-card {
  max-width: 600px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 